package org.mspring.mlog.security.service;

import java.util.List;

import org.mspring.mlog.dao.TreeItemDao;
import org.mspring.mlog.entity.TreeItem;
import org.mspring.nbee.orm.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.orm.dao.sql.pager.PageResult;
import org.mspring.nbee.orm.dao.sql.pager.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2014-09-10 12:44:49
 */
@Service
public class TreeItemService {

    private TreeItemDao treeItemDao;

    public TreeItem findById(String id) {
        return treeItemDao.findById(id);
    }

    public PageResult<TreeItem> list(List<SqlCondition> conditions, Pager pager) {
        return treeItemDao.listPage(conditions, pager);
    }

    /**
     * 根据角色查找角色对应的树形菜单
     *
     * @param roleId
     * @return
     */
    public List<TreeItem> listByRole(long roleId) {
        return treeItemDao.listByRole(roleId);
    }

    /**
     * 加载树形菜单，Type为TreeItem.Type.TREE_FOLDER, TreeItem.Type.TREE_ITEM
     *
     * @param userId
     * @return
     */
    public List<TreeItem> listTreeByUser(long userId) {
        return treeItemDao.listTreeByUser(userId);
    }

    /**
     * 加载tab页签
     *
     * @param parent
     *            父节点编号
     * @param userId
     *            用户编号
     * @return
     */
    public List<TreeItem> listTabByUser(String parent, long userId) {
        return treeItemDao.listTabByUser(parent, userId);
    }

    /**
     * 加载树形菜单节点
     *
     * @param parent
     *            父节点编号
     * @param userId
     *            用户编号
     * @return
     */
    public TreeItem getOpenTab(String parent, long userId) {
        return treeItemDao.getOpenTab(parent, userId);
    }

    public List<TreeItem> listAll() {
        return treeItemDao.listAll();
    }

    @Transactional
    public boolean update(TreeItem treeItem) {
        return treeItemDao.update(treeItem);
    }

    @Transactional
    public boolean deleteById(long id) {
        return treeItemDao.deleteById(id);
    }

    @Autowired
    public void setTreeItemDao(TreeItemDao treeItemDao) {
        this.treeItemDao = treeItemDao;
    }

}
